/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */
package net.stuff.servoy.beans.svg;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGDocument;

/**
 * @author Servoy Stuff
 * http://www.servoy-stuff.net/
 */
public class SVGLoader {

	public SVGDocument createSVGDocumentFromPath(String path) throws SVGLoadException {
		
		SVGDocument svgDoc = null;
		
		if (path != null) {
			SAXSVGDocumentFactory factory = getDocumentFactory();
		    // We can create from a document from a SVG from a string corresponding to a URI
		    // We convert svgPath into a URI
		    File svgFile = new File(path);
		    // We get the URI corresponding to svgFile
		    URI svgURI = null;
		    try
		    {
		    	svgURI = svgFile.toURI();
		    } catch (SecurityException ex) {
		    	svgURI = null;
		    	throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.toURI")+path + "\n"+ ex.getLocalizedMessage(), ex);
		    }
		    if (svgURI != null) {
		    	Document doc=null;
		    	try {
		    		doc = factory.createDocument(svgURI.toString());
		    	} catch (IOException ex) {
		    		doc = null;
		    		throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.createDocument")+path + "\n"+ ex.getLocalizedMessage(), ex);
		    	}
		    	if (doc != null){
		    		svgDoc = (SVGDocument)doc;
		    	}
		    }
		} else {
			throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.filePathNull"));
		}
		return svgDoc;
	}
	
	public URL getURL(String urlString) throws SVGLoadException {
		URL url = null;
		if (urlString != null) {
			try {
				url = new URL(urlString);
			} catch (MalformedURLException ex) {
	    		throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.URL")+urlString + "\n"+ ex.getLocalizedMessage(), ex);
			}		
		} else {
			throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.URLNull"));
		}
		return url;
	}
	
	public InputStream getInputStream(URL url) throws SVGLoadException {
		InputStream is = null;
		if (url != null) {
			try {
				is = url.openStream();
			} catch (IOException ex) {
	    		is = null;
	    		throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.openStream")+url.toExternalForm() + "\n"+ ex.getLocalizedMessage(), ex);
			}			
		} else {
			throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.URLNull"));
		}
		return is;
	}

	public SVGDocument createSVGDocumentFromInputStream(URL url, InputStream is) throws SVGLoadException {
		SVGDocument svgDoc = null;
		if (url != null) {
			if (is != null) {
				SAXSVGDocumentFactory factory = getDocumentFactory();
				Document doc = null;
				try {
					doc = factory.createDocument(url.toURI().toString(), is);
				} catch (Exception ex) {
		    		doc = null;
		    		throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.createDocumentURL")+url.toExternalForm() + "\n"+ ex.getLocalizedMessage(), ex);						
				} finally {
					if (is != null) {
						try {
							is.close();
						} catch (IOException ex) {}
					}
				}
				if (doc != null){
		    		svgDoc = (SVGDocument)doc;
		    	}
			} else {
	    		throw new SVGLoadException(MessageBundle.getString("evt.svgLoad.error.connectionNotOpen")+url.toExternalForm());				
			}
		} else {
			throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.URLNull"));
		}
		return svgDoc;
	}
	
	public SVGDocument createSVGDocumentFromXML(String xml, String uri) throws SVGLoadException {
		SVGDocument svgDoc = null;
		if (xml != null) {
			SAXSVGDocumentFactory factory = getDocumentFactory();
			Document doc = null;
			try {
				StringReader sr = new StringReader(xml);
				doc = factory.createDocument(uri, sr);
			} catch (Exception ex) {
	    		throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.createDocumentXML")+"\n"+ ex.getLocalizedMessage(), ex);
			}
			if (doc != null){
	    		svgDoc = (SVGDocument)doc;
	    	}
		} else {
			throw new SVGLoadException(MessageBundle.getString("evt.svg.svgLoader.error.URLNull"));
		}
		return svgDoc;
	}

	private SAXSVGDocumentFactory getDocumentFactory() {
		// We get the parser class name
		String parserClassName = XMLResourceDescriptor.getXMLParserClassName();
		// We get a SAX SVG Document Factory instance
		SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parserClassName);
		return factory;
	}

}
